package org.zfes.snowier.zbss.xzy.biz.complaint.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.AppCtxUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;
import org.zfes.snowier.zbss.xzy.biz.complaint.service.IComplaintReplayService;
import org.zfes.snowier.zbss.xzy.biz.complaint.service.IComplaintService;
import org.zfes.snowier.zbss.xzy.biz.dto.ComplaintDto;
import org.zfes.snowier.zbss.xzy.complain.ComplaintCreateDto;
import org.zfes.snowier.zbss.xzy.biz.complaint.dao.ComplaintMapper;
import org.zfes.snowier.zbss.xzy.biz.complaint.model.Complaint;
import org.zfes.snowier.zbss.xzy.biz.complaint.model.ComplaintReplay;

import java.util.stream.Stream;
@Service
public class ComplaintServiceImpl implements IComplaintService{
 	@Autowired
	private ComplaintMapper complaintMapper;
 	IComplaintReplayService complaintReplayService;
 	
	public IComplaintReplayService getComplaintReplayService() {
		if(complaintReplayService==null) {
			complaintReplayService=AppCtxUtil.cfg.getBean(IComplaintReplayService.class);
		}
		return complaintReplayService;
	}
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createComplaint(ComplaintCreateDto complaintDto) {
	
		ZBeanUtil.validateBean(complaintDto);
		
		Complaint complaint=new Complaint();
		ZBeanUtil.copy(complaintDto, complaint);
		
		ZBeanUtil.setCreateInfo(complaint);
		complaintMapper.insertSelective(complaint);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ComplaintDto> loadComplaintById(Long id) {
		if(id!=null){
			
			Complaint complaint=complaintMapper.selectByPrimaryKey(id);
			if(complaint==null) {
				return Optional.empty();
			}
			List<ComplaintReplay> replays= getComplaintReplayService().loadByComplaintId(id);
			return Optional.ofNullable(new ComplaintDto().setComplaint(complaint).setReplays(replays));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Complaint> loadById(Long id) {
		if(id!=null){
			return Optional.ofNullable(complaintMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateComplaint(Complaint complaint) {
		ZBeanUtil.setUpdatedInfo(complaint);
		ZBeanUtil.validateBean(complaint);
		complaintMapper.updateByPrimaryKeySelective(complaint);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteComplaint(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		complaintMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setComplaintStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		complaintMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void userSetComplaintStatus(Long userId,Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		loadById( id).ifPresent(c->{
			ZAssert.equals(c.getUserId(), userId, "您没有权限设置该数据");
			complaintMapper.updateStatus( id,  status) ;
		});
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadComplaintDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(complaintMapper.selectListCount(pm), complaintMapper.selectMapListPage(pm));
		return ds;
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadComplaintSelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=complaintMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}
    
}